package cn.mysdp.biz.facade;


import cn.mysdp.biz.convert.*;
import cn.mysdp.biz.domain.*;
import cn.mysdp.biz.dto.request.*;
import cn.mysdp.biz.dto.response.*;
import cn.mysdp.biz.repository.*;
import cn.mysdp.biz.facade.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: SdpTemplateFacadeBaseImpl
 * @Description:
 * @Author: SDP
 * @Date: 2021-10-30
 * @Version: 1.0
 * Table: sdp_template
 * Comment:
 * 
 */
public class SdpTemplateFacadeBaseImpl extends BaseServiceImpl implements SdpTemplateFacade {
    @Autowired
    protected SdpTemplateMapper sdpTemplateMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        map.put("unique_template_index_3", "workspace_name-project_name-name-file_type-project-package_name");
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addSdpTemplate(SdpTemplateAddRequest request) throws Exception {
        SdpTemplateWithBLOBs daoRequest = new SdpTemplateAddRequestConvert().convert(request);
        fixEqualCondition(request, "sdp_template", "add", "", null);
        Integer result = 0;
        try {
            result = sdpTemplateMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "sdp_template", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateSdpTemplate(SdpTemplateUpdateRequest request) throws Exception {
        fixEqualCondition(request, "sdp_template", "update", request.getId()+"", null);
        checkInternalId(request, "sdp_template", "update", request.getId());
        addHistory(request, "U", "sdp_template", "api", request.getId(), null);

        SdpTemplateWithBLOBs daoRequest = new SdpTemplateUpdateRequestConvert().convert(request);
        SdpTemplateExample example = new SdpTemplateExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), SdpTemplateExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = sdpTemplateMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteSdpTemplate(SdpTemplateDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "sdp_template", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "sdp_template", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "sdp_template", "api", request.getIdList().get(index), null);
            sdpTemplateMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public SdpTemplateQueryResponse querySdpTemplate(SdpTemplateQueryRequest request) throws Exception {
        SdpTemplateWithBLOBs daoResponse = sdpTemplateMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        SdpTemplateQueryResponse response = new SdpTemplateQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<SdpTemplateQueryResponse> listSdpTemplate(SdpTemplateQueryRequest request) throws Exception {
        SdpTemplateWithBLOBs daoRequest = new SdpTemplateWithBLOBs();
        copyProperties(request, daoRequest);
        SdpTemplateExample example;
        try {
            example = SdpTemplateExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<SdpTemplateWithBLOBs> list;
        try {
            list = sdpTemplateMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<SdpTemplateQueryResponse> responseList = new ArrayList<>();
        for(SdpTemplateWithBLOBs item : list) {
            SdpTemplateQueryResponse newItem = new SdpTemplateQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countSdpTemplate(SdpTemplateQueryRequest request) throws Exception {
        SdpTemplateWithBLOBs daoRequest = new SdpTemplateWithBLOBs();
        copyProperties(request, daoRequest);
        SdpTemplateExample example;
        try {
            example = SdpTemplateExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = sdpTemplateMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statSdpTemplate(SdpTemplateQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
